clc;
clear all;
% Starting time:
% Finishing time:

% AT Target, contain Local Totally Situation
% Scheme2：Golden Hungarian Scheme（Propose）

%% Parameter settings
%%% Environment %%%
B = 2*10^6;
noise_p = 10^(-9);
% Helpers Distribution: 0~4 m 、  Users Distribution: 4~7 m
r_EN_large = 7;
r_EN_small = 4;

%%% Energy Node %%%
eff = 0.67;
P_w = 3;

%%% User %%%
num_u = 3;
bit_to_cycle = 800*ones(1,num_u);
Data = 30*(10^3)*ones(1,num_u);
cpu = 10^(-28);

%%% Helper %%%
num_h_3 = 8;

%% Algorithm
iter_max = 20;

% Scheme2：Golden Hungarian Scheme
GoldenHungarian_final_8h_exp = zeros(1,iter_max);

% Monte Calor
run_total = 500;
GoldenHungarian_final_8h_run = zeros(iter_max,run_total);


for run = 1:run_total
    [d_wu_3,d_wh_3,d_uh_3] = situation(num_u,num_h_3,r_EN_large,r_EN_small);
    % EN到Users信道
    h_large_wu_3 = (10^(-3))*(d_wu_3.^(-3));
    h_wu_3 = zeros(1,num_u);
    for n = 1:num_u
        h_wu_3(n) = h_large_wu_3(n)*Rayleighchannel();
    end
    H_wu_3 = h_wu_3;
    % EN到Helpers_3信道
    h_large_wh_3 = (10^(-3))*(d_wh_3.^(-3));
    h_wh_3 = zeros(1,num_h_3);
    for m = 1:num_h_3
        h_wh_3(m) = h_large_wh_3(m)*Rayleighchannel();
    end
    H_wh_3 = h_wh_3;
    % Users到Helpers_3信道
    h_large_uh_3 = (10^(-3))*(d_uh_3.^(-3));
    h_uh_3 = zeros(num_u,num_h_3);
    for n = 1:num_u
        for m = 1:num_h_3
            h_uh_3(n,m) = h_large_uh_3(n,m)*Rayleighchannel();
        end
    end
    H_uh_3 = h_uh_3./noise_p;

    disp('GH_8h');disp(run);
    [GoldenHun_8h_FinalIter] = GoldenHungarian_iteriter_CL(iter_max,B,num_u,num_h_3,H_wu_3,H_wh_3,H_uh_3,eff,P_w,bit_to_cycle,cpu,Data);
    GoldenHungarian_final_8h_run(:,run) = GoldenHun_8h_FinalIter(:)/num_u;
end

for iter = 1:iter_max
    GoldenHungarian_final_8h_exp(iter) = sum(GoldenHungarian_final_8h_run(iter,:))/run_total;
end

iter_exp = [2:2:20];
GoldenHungarian_final_8h_exp_pick = zeros(1,length(iter_exp));

for ii = 1:length(iter_exp)
    iii = iter_exp(ii);
    GoldenHungarian_final_8h_exp_pick(ii) = GoldenHungarian_final_8h_exp(iii);
end

figure(10);
plot(iter_exp(:),GoldenHungarian_final_8h_exp_pick(:),'--sb','LineWidth',1.7,'MarkerSize',7); grid on; hold on;
legend('空闲用户数量为8，所提高性能方案');
xlabel('迭代次数');
ylabel('系统平均任务完成时延/s');